﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;

namespace MYTech.Data
{
    /// <summary>
    /// 字符串帮助类
    /// </summary>
    public class StringHelper
    {
        /// <summary>
        /// 获取指定字符编码下字符串的一个字节序列。
        /// </summary>
        /// <param name="val">字符串</param>
        /// <param name="encoding">编码</param>
        /// <returns></returns>
        public static byte[] GetBytes(string val, Encoding encoding)
        {
            byte[] buff = encoding.GetBytes(val);
            return buff;
        }

        /// <summary>
        /// 获取默认字符编码下字符串的一个字节序列。
        /// </summary>
        /// <param name="val">字符串</param>
        /// <returns></returns>
        public static byte[] GetBytes(string val)
        {
            return GetBytes(val, Encoding.Default);
        }

        /// <summary>
        /// 对字符串类型的字节序列进行解码,转化为对应的字符串信息
        /// </summary>
        /// <param name="allBytesStr"></param>
        /// <param name="style"></param>
        /// <param name="encoding"></param>
        /// <param name="splitChar">字符串分隔符</param>
        /// <returns></returns>
        public static string GetString(string allBytesStr, NumberStyles style, Encoding encoding, params char[] splitChar)
        {
            string[] subBytes = allBytesStr.Split(splitChar);
            return GetString(subBytes, style, encoding);
        }

        /// <summary>
        /// 对字符串类型的字节序列数组进行解码,转化为对应的字符串信息
        /// </summary>
        /// <param name="subBytes"></param>
        /// <param name="style"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static string GetString(string[] subBytes, NumberStyles style, Encoding encoding)
        {
            byte[] buff = new byte[subBytes.Length];
            for (int i = 0; i < subBytes.Length; i++)
            {
                byte _byte = new byte();
                if (GetEqualByte(subBytes[i], style, out _byte))
                {
                    buff[i] = _byte;
                }
                else
                    return null;
            }
            if (buff == null)
                return null;
            return encoding.GetString(buff);
        }

        public static byte[] GetBytes(string allBytesStr, NumberStyles style, Encoding encoding, params char[] splitChar)
        {
            string[] subBytes = allBytesStr.TrimEnd().Split(splitChar);
            byte[] buff = new byte[subBytes.Length];
            for (int i = 0; i < subBytes.Length; i++)
            {
                byte _byte = new byte();
                if (GetEqualByte(subBytes[i], style, out _byte))
                {
                    buff[i] = _byte;
                }
                else
                    return null;
            }
            return buff;
        }

        public static string GetBytesStr(byte[] buff, char splitChar)
        {
            StringBuilder sb = new StringBuilder();
            foreach (byte _val in buff)
            {
                sb.AppendFormat("{0:X} ", _val);
            }
            return sb.ToString();
        }

        /// <summary>
        /// 对字符串类型的字节序列数组进行解码,转化为对应的字符串信息
        /// </summary>
        /// <param name="subBytes"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static string GetString(string[] subBytes, Encoding encoding)
        {
            return GetString(subBytes, NumberStyles.AllowHexSpecifier, encoding);
        }

        /// <summary>
        /// 默认字符编码类型对字符串类型的字节序列数组进行解码,转化为对应的字符串信息
        /// </summary>
        /// <param name="subBytes"></param>
        /// <returns></returns>
        public static string GetString(string[] subBytes)
        {
            return GetString(subBytes, Encoding.Default);
        }

        /// <summary>
        /// 转化字节型字符串到字节，成功是返回true，否则false
        /// </summary>
        /// <param name="byteStr"></param>
        /// <param name="style"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public static bool GetEqualByte(string byteStr, NumberStyles style, out byte result)
        {
            return byte.TryParse(byteStr, style, null, out result);
        }

        /// <summary>
        /// 转化字节型字符串到字节，成功是返回true，否则false
        /// </summary>
        /// <param name="byteStr"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public static bool GetEqualByte(string byteStr, out byte result)
        {
            return GetEqualByte(byteStr, NumberStyles.AllowHexSpecifier, out result);
        }

        /// <summary>
        /// 字符串反向
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string Reverse(string str)
        {
            StringBuilder sb = new StringBuilder();
            char[] chars = str.ToCharArray();
            for (int i = chars.Length - 1; i >= 0; i--)
            {
                sb.Append(chars[i]);
            }
            return sb.ToString();
        }
    }
}
